package com.yanqu.road.dao.impl.activity.springunionbanquet;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.springunionbanquet.SpringUnionBanquetOpenData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class SpringUnionBanquetOpenDataDaoImpl extends TempDataObjectDao<SpringUnionBanquetOpenData> {

    public long getMaxBanquetId(int activityId) {
        String sql = "select ifnull(max(banquetId),0) as maxId from `t_c_spring_union_banquet_open` where `activityId`=?";
        ResultSet rs = null;
        CallableStatement call = null;
        long maxId = 0L;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getLong("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }

    public static String getSpringUnionBanquetOpenDataSql = "select * from `t_c_spring_union_banquet_open` where `activityId`=?";
    public Map<Long, SpringUnionBanquetOpenData> getSpringUnionBanquetOpenData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, SpringUnionBanquetOpenData> result = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getSpringUnionBanquetOpenDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SpringUnionBanquetOpenData data =  parse(rs);
                result.put(data.getBanquetId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateSpringUnionBanquetOpenDataSql = "update `t_c_spring_union_banquet_open` set openUserId=?,openTime=?,unionUid=?,endTime=?,resource=?,configId=?,maxLimitCount=?,specialTotalCount=?,specialCount=?,specialPoolList=?,joinIdList=?,poolList=? where activityId=? and banquetId=?";
    @Override
    protected boolean updateData(SpringUnionBanquetOpenData openData) {
        openData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, openData.getOpenUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, openData.getOpenTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, openData.getUnionUid()));
        para.put(i++, new DbParameter(Types.BIGINT, openData.getEndTime()));
        para.put(i++, new DbParameter(Types.INTEGER, openData.getResource()));
        para.put(i++, new DbParameter(Types.INTEGER, openData.getConfigId()));
        para.put(i++, new DbParameter(Types.INTEGER, openData.getMaxLimitCount()));
        para.put(i++, new DbParameter(Types.INTEGER, openData.getSpecialTotalCount()));
        para.put(i++, new DbParameter(Types.INTEGER, openData.getSpecialCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(openData.getSpecialPoolList(), "|")));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(openData.getJoinIdList(), ",")));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(openData.getPoolList(), "|")));
        para.put(i++, new DbParameter(Types.INTEGER, openData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, openData.getBanquetId()));
        if (getDb().executeNoneQuery(updateSpringUnionBanquetOpenDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSpringUnionBanquetOpenDataSql = "insert into `t_c_spring_union_banquet_open`(activityId,banquetId,openUserId,openTime,unionUid,endTime,resource,configId,maxLimitCount,specialTotalCount,specialCount,specialPoolList,joinIdList,poolList) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(SpringUnionBanquetOpenData openData) {
        openData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, openData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, openData.getBanquetId()));
        para.put(i++, new DbParameter(Types.BIGINT, openData.getOpenUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, openData.getOpenTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, openData.getUnionUid()));
        para.put(i++, new DbParameter(Types.BIGINT, openData.getEndTime()));
        para.put(i++, new DbParameter(Types.INTEGER, openData.getResource()));
        para.put(i++, new DbParameter(Types.INTEGER, openData.getConfigId()));
        para.put(i++, new DbParameter(Types.INTEGER, openData.getMaxLimitCount()));
        para.put(i++, new DbParameter(Types.INTEGER, openData.getSpecialTotalCount()));
        para.put(i++, new DbParameter(Types.INTEGER, openData.getSpecialCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(openData.getSpecialPoolList(), "|")));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(openData.getJoinIdList(), ",")));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(openData.getPoolList(), "|")));
        if (getDb().executeNoneQuery(insertSpringUnionBanquetOpenDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SpringUnionBanquetOpenData parseData(ResultSet rs) throws SQLException {
        SpringUnionBanquetOpenData openData = new SpringUnionBanquetOpenData();
        openData.setActivityId(rs.getInt("activityId"));
        openData.setBanquetId(rs.getLong("banquetId"));
        openData.setOpenUserId(rs.getLong("openUserId"));
        openData.setOpenTime(rs.getLong("openTime"));
        openData.setUnionUid(rs.getString("unionUid"));
        openData.setEndTime(rs.getLong("endTime"));
        openData.setResource(rs.getInt("resource"));
        openData.setConfigId(rs.getInt("configId"));
        openData.setMaxLimitCount(rs.getInt("maxLimitCount"));
        openData.setSpecialCount(rs.getInt("specialCount"));
        openData.setSpecialTotalCount(rs.getInt("specialTotalCount"));

        String poolList = rs.getString("poolList");
        openData.setPoolList(StringUtils.stringToStringList(poolList, "\\|"));

        String sepcialPoolList = rs.getString("specialPoolList");
        openData.setSpecialPoolList(StringUtils.stringToStringList(sepcialPoolList, "\\|"));

        openData.setJoinIdList(StringUtils.stringToLongList(rs.getString("joinIdList"), ","));

        return openData;
    }
}
